package com.hxzy.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;

public class DataSourceHelper {
    private static final String PATH="druid.properties";
    public static Properties load(String path){
        //通过类加载器，从本地classpath路基下加载druid.properties文件
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
        //继承了HashTable的类；HashTable实现了Map接口
        //Properties已键值对的方式保存元素
        Properties properties=new Properties();
        try {
            //加载配置文件
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }
    //读取配置文件都得到一个Properties
    public  static  Properties add(String path){
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
        Properties properties=new Properties();
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }
    //
    public  static DataSource getDateSource(){
        DataSource dateSources=null;

        try {
            //读取配置文件
            Properties properties=load(PATH);
            /*
             * 通过druid提供工厂方法去生成一个DataSource连接池
             * properties中的键必须要和DruidDataSource的键一致
             * */
            dateSources=DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateSources;
    }


}
